package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/6 10:31
 * @description：https://leetcode-cn.com/problems/set-mismatch/
 */
public class 错误的集合 {
    @Test
    public void test() {
        Solution solution = new Solution();
        int[] errorNums = solution.findErrorNums(new int[]{3, 2, 3, 4, 6, 5});
        System.out.println(Arrays.toString(errorNums));
    }

    class Solution {
        public int[] findErrorNums(int[] nums) {
            int[] errorNums = new int[2];
            int n = nums.length;
            Arrays.sort(nums);
            int prev = 0;
            for (int i = 0; i < n; i++) {
                int cur = nums[i];
                if (cur == prev) {
                    errorNums[0] = cur;
                } else if (cur - prev > 1) {
                    errorNums[1] = prev + 1;
                }
                prev = cur;
            }
            if (errorNums[1] == 0) {
                errorNums[1] = n;
            }
            return errorNums;
        }
    }
}
